<META http-equiv="refresh" content="<?php echo rand(5,10); ?>" />
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{font-size:12px;}
td{font-size:12px;}
</style>
<?php


/**
 * 数据采集
 */
header('Content-type: text/html; charset=UTF-8');

include_once 'common/tools.php';
include_once 'module/simple_html_dom.php';

$matchId = $_GET["matchId"];
if ($matchId == "") {
	return;
}

$matchDetail = file_get_html("http://nba.sports.sina.com.cn/scroll_iframe.php?id=" . $matchId . "&quarter=all");
$matchDetail = $matchDetail->find('table tr');

//先清空当前比赛场次的数据
$sql = "delete from live_score where matchId = '" . $matchId . "';";
$sql .= "INSERT INTO live_score (scoreId, matchId, homeTeamScore, visitTeamScore, scoreTime, scoreAction) VALUES";

$id = 0;
$match_q = 0;
$match_st = "第0节";
$match_m = "未开赛";
$match_s1 = 1000;
$match_s2 = 1000;

$match_show_m = null;
$match_show_s = null;
$match_show_t = null;
$match_show_s1 = null;
$match_show_s2 = null;

$sql_values = array ();

$match_st_array = array ();
$match_st_array_index = 0;

foreach ($matchDetail as $match_tr) {

	$match_score = $match_tr->find('td', 4)->innertext;
	$match_score = str_replace("[", "", $match_score);
	$match_score = str_replace("]", "", $match_score);
	$match_score = explode("-", $match_score);
	if ($match_tr->find('td', 1)->innertext == "") {
		continue;
	}
	if ($match_tr->find('td', 2)->innertext != "") {

		//取得比赛内容
		$match_m = $match_tr->find('td', 2)->innertext;
		$match_m = preg_replace("/<a [^>]*>|<\/a>/", "", $match_m); //去除超级连接
		$match_m = iconv("GBK", "UTF-8", $match_m);

		//取得赛时
		if (strpos($match_m, "第") === 0 && strpos($match_m, "节开始") === 4) {
			$match_st_array[$match_st_array_index++] = $id;
			if ($match_show_s == null) {
				$match_show_s = substr($match_m, 0, 7);
			}
		}

		$match_st = "第" . $match_q . "节\n" . str_replace(".", "", $match_tr->find('td', 0)->innertext);

		if ($match_show_m == null) {
			$match_show_m = $match_m;
			$match_show_t = $match_tr->find('td', 0)->innertext;
		}
		if ($match_show_s1 == null) {
			$match_show_s1 = $match_score[0];
		}
		if ($match_show_s2 == null) {
			$match_show_s2 = $match_score[1];
		}
	} else {
		continue;
	}


	$match_s1 = $match_score[0];
	$match_s2 = $match_score[1];
	$id++;
	
	$sql_values[$id] = "'" . $matchId . "', '" . $match_s1 . "','" . $match_s2 . "', '" . $match_st . "', '" . $match_m . "'),";
}

//重新排序比赛.
$match_st_array_index = 0;
foreach ($sql_values as $i => $value) {
	if ($match_st_array_index < count($match_st_array)) {
		if ($match_st_array[$match_st_array_index] == $i) {
			$match_st_array_index = $match_st_array_index +1;
		}
	}
	$value = str_replace("第0节", "第" . (count($match_st_array) - $match_st_array_index) . "节", $value);
	$sql .= "('" . ($id - $i +1) . "'," . $value;
}

echo "<table width='100%'>";
echo "<tr>";
echo "<td>" . $id . "&nbsp;&nbsp;&nbsp;&nbsp;" . $match_show_s . " " . $match_show_t . "&nbsp;&nbsp;&nbsp;&nbsp;" . $match_show_s1 . " : " . $match_show_s2 . "</td>";
echo "</tr><tr>";
echo "<td>" . $match_show_m . "</td>";
echo "</tr>";
echo "</table>";
$sql = "SET NAMES utf8;" . substr($sql, 0, strlen($sql) - 1) . ";"; //插入比赛比分
if ($id > 0) {

	//连接数据库
	$config = require 'conf/config.ini.php'; //加载公用config.ini.php
	$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);
	
	//插入比赛比分
	mysqli_multi_query($conn, $sql);
}
?>